import 'package:battery_plugin/battery_plugin.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class BatterPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _BatteryPageState();
  }
}

class _BatteryPageState extends State<BatterPage> {
  static const String channel = 'samples.flutter.io/battery';
  String _batteryLevel = 'Unknown battery level.';

  static const platform = const MethodChannel(channel);

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('battery'),
      ),
      body: _body(),
    );
  }

  Widget _body() {
    return Column(
      children: <Widget>[
        Container(
          color: Colors.redAccent[100],
          width: double.infinity,
          padding: EdgeInsets.all(10),
          child: Text(
            '电量:$_batteryLevel',
            textAlign: TextAlign.center,
          ),
        ),
        OutlineButton(
          onPressed: () {
            _getBatteryLevel();
          },
          child: Text('获取🔋电量🐼§😄'),
        ),
        OutlineButton.icon(
          onPressed: _bugly,
          icon: Icon(
            Icons.error,
            color: Colors.red,
          ),
          label: Text('bugly上传'),
        ),
      ],
    );
  }

  _bugly() {
    String s = null;
    try {
      int.parse(s);
    } catch (e) {
      print(e);
    }
  }

  Future<Null> _getBatteryLevel() async {
    String batteryLevel = await BatteryPlugin.platformVersion;
//    try {
//      final int result = await platform.invokeMethod('getBatteryLevel');
//      batteryLevel = 'Battery level at $result % .';
//    } on PlatformException catch (e) {
//      batteryLevel = "Failed to get battery level: '${e.message}'.";
//    }

    setState(() {
      _batteryLevel = batteryLevel;
    });
  }
}
